# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=mitra
pkgver=3.8.0
pkgrel=0
_mitraweb="${pkgver%.*}.0"
pkgdesc="ActivityPub microblogging platform written in Rust"
url="https://mitra.social/@mitra"
# loongarch64, ppc64le, riscv64, s390x: mitra v3 removed native-tls feature
# armhf, armv7, x86: starting with v3.4.0 - "literal out of range for usize"
arch="aarch64 x86_64"
license="AGPL-3.0-only"
depends="postgresql"
makedepends="
	cargo
	cargo-auditable
	nodejs
	npm
	"
install="$pkgname.pre-install $pkgname.post-install"
pkgusers="mitra"
pkggroups="mitra"
subpackages="$pkgname-doc $pkgname-openrc"
source="mitra-$pkgver.tar.gz::https://codeberg.org/silverpill/mitra/archive/v$pkgver.tar.gz
	mitra-web-$_mitraweb.tar.gz::https://codeberg.org/silverpill/mitra-web/archive/v$_mitraweb.tar.gz
	mitra.initd
	init.sql
	config.yaml
	"
builddir="$srcdir/mitra"

_cargo_opts="--frozen --no-default-features --features production"

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked

	cd "$srcdir/mitra-web"

	npm ci --foreground-scripts
}

build() {
	RUSTFLAGS="--cfg=ammonia_unstable" \
	cargo auditable build $_cargo_opts --release

	cd "$srcdir/mitra-web"

	echo 'VITE_BACKEND_URL=' > .env.local
	npm run build
}

check() {
	# These tests require a database connection
	RUSTFLAGS="--cfg=ammonia_unstable" \
	cargo test $_cargo_opts --workspace \
		--exclude mitra_models -- \
		--skip test_follow_unfollow \
		--skip test_hide_reblogs \
		--skip test_subscribe_unsubscribe \
		--skip test_get_jrd \
		--skip test_filter_mentions_none \
		--skip test_filter_mentions_only_known \
		--skip test_prepare_instance_ed25519_key \
		--skip test_mute \
		--skip test_federation_filter \
		--skip test_parse_content_object_link_and_mention

	cd "$srcdir/mitra-web"

	npm run test
}

package() {
	install -Dm755 target/release/mitra -t "$pkgdir"/usr/bin
	install -Dm755 target/release/mitractl -t "$pkgdir"/usr/bin

	mkdir -p "$pkgdir"/usr/share/webapps
	cp -r "$srcdir"/mitra-web/dist \
		"$pkgdir"/usr/share/webapps/mitra

	install -Dm644 docs/* -t "$pkgdir"/usr/share/doc/$pkgname
	install -Dm644 config_dev.example.yaml \
		contrib/Caddyfile contrib/*.nginx \
		contrib/monero/wallet.conf \
		-t "$pkgdir"/usr/share/doc/$pkgname/examples

	install -Dm640 -g mitra "$srcdir"/config.yaml -t "$pkgdir"/etc/mitra
	install -dm755 -o mitra -g mitra "$pkgdir"/var/lib/mitra
	install -Dm644 "$srcdir"/init.sql -t "$pkgdir"/var/lib/mitra
	install -Dm755 "$srcdir"/mitra.initd "$pkgdir"/etc/init.d/mitra
}

sha512sums="
84cbbc4c43276ebcf8f5aee3da0f7b5888d1ccacfa982e96d94b249fe885c4870a951a1f20f0de8d9481f3f25d50208a0f3858d9b162b969130cede9be27cc9f  mitra-3.8.0.tar.gz
2f77c8d9eec26b6b582f1224f06624a3bdedff965c402d0052633e60553dfd65470371d036e960278ee677997a4c5d9b13f9e34364d7e45296fd06f58d571643  mitra-web-3.8.0.tar.gz
691f84f5dfdddc176e75792ab03ff167054246e75ced51be47a89f405ae55ebe5eb6280b73c1b467b5ecbe8539f6108fb3d86873d50fcc4f4b8c5b182632acb0  mitra.initd
180a47f5072534418b4aac3ce7c885a4f7e4dc38aca80d6d81c79848d12fbe24799788c3575bd195030a10da5e0372f87fa2809a4ef99a48eaa6df52f4d053dd  init.sql
c921efdf7e22bcb9e5adc8e84cf657e87611eab96571e43c25037463148f2ab0c3c5ae1d748c041db42560553ccb1e1f96bb6e858d7da0761cbad366b391f924  config.yaml
"
